import 'package:flutter/material.dart';
import 'package:flutter_boost/flutter_boost.dart';
import 'package:setting_flutter/order/index.dart';

//第一步:创建一个自定义的bingding,继承和with关系,然后里面什么都不用写
class CustomFlutterBinding extends WidgetsFlutterBinding
    with BoostFlutterBinding {}

void main() {
  //第二步:需要引用这个binding
  CustomFlutterBinding();
  runApp(const MyApp());
}

//第三步:创建路由表
class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Map<String, FlutterBoostRouteFactory> routerMap = {
    //第一个路由表
    '/OrderDetail': (settings, isContainerPage, uniqueId) {
      return MaterialPageRoute(
        settings: settings,
        builder: (BuildContext ctx) {
          return const OrderPage();
        },
      );
    },
    //第二个路由表
  };

  // 第四步:路由工厂函数(固定写法,cv即可)
  Route<dynamic> routeFactory(
      RouteSettings settings, bool isContainerPage, String? uniqueId) {
    FlutterBoostRouteFactory? fn = routerMap[settings.name];
    if (fn == null) {
      throw FlutterError(
          'Route "${settings.toString()}" is not defined in routerMap.');
    }
    return fn(settings, isContainerPage, uniqueId)!;
  }

//第五步:flutter_boost接管鸿蒙项目(固定写法,cv即可)
  @override
  Widget build(BuildContext context) {
    // flutter_boost 接管
    return FlutterBoostApp(
      routeFactory,
      // Flutter 侧直接预览需要，需要使用 Deveco Studio 导入 .ohos 项目进行自动签名预览至鸿蒙设备
      initialRoute: '/OrderDetail',
      appBuilder: (home) {
        return MaterialApp(
          builder: (context, child) => home,
        );
      },
    );
  }
}
